package org.vy.dal.dao;

import java.util.List;

import org.vy.dal.QueryManager;

public class VyAshramDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE ASHRAM_CODE = m_AshramCode";
	private static final String FIND_BY_PARAMETER = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String UPDATE = "UPDATE <TABLE> SET ASHRAM_NAME=m_AshramName,ASHRAM_ADDRESS_ID=m_AshramAddress,ASHRAM_CONTACT_NO=m_AshramContactNo,EMAIL=m_Email,PRIMARY_CONTACT=m_PrimaryContact,SECONDARY_CONTACT=m_SecondaryContact,FAX=m_Fax,START_DATE=m_StartDate,OWN_OR_RENT=m_OwnOrRent,IS_DELETED=m_IsDeleted,COMMENT=m_Comment,LAST_UPDATED_BY=m_LastUpdatedBy WHERE ASHRAM_CODE = m_AshramCode";
	private static final String INSERT = "INSERT INTO <TABLE> (ASHRAM_CODE, ASHRAM_NAME, ASHRAM_ADDRESS_ID, ASHRAM_CONTACT_NO, EMAIL, PRIMARY_CONTACT, SECONDARY_CONTACT, FAX, START_DATE, OWN_OR_RENT, IS_DELETED, COMMENT, LAST_UPDATED_BY, LAST_UPDATED_ON) VALUES(m_AshramCode,m_AshramName,m_AshramAddress,m_AshramContactNo,m_Email,m_PrimaryContact,m_SecondaryContact,m_Fax,m_StartDate,m_OwnOrRent,m_IsDeleted,m_Comment,m_LastUpdatedBy,m_LastUpdatedOn)";
	private static final String DELETE = "DELETE FROM <TABLE> WHERE ASHRAM_CODE = m_AshramCode";

	public static VyAshramDO findByPK(String key) throws Exception {
		VyAshramDO dto = new VyAshramDO();
		dto.setAshramcode(key);
		dto = (VyAshramDO) QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	public static VyAshramDO insert(VyAshramDO dto) throws Exception {

		dto = (VyAshramDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyAshramDO update(VyAshramDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyAshramDO delete(String key) throws Exception {
		VyAshramDO dto = new VyAshramDO();
		dto.setAshramcode(key);
		boolean i = QueryManager.executeDelete(dto, DELETE);
		if (i == true)
			return dto;
		else
			return null;
	}

	@SuppressWarnings("unchecked")
	public static List<VyAshramDO> findByParam(VyAshramDO dto) throws Exception {

		StringBuilder sb = new StringBuilder(FIND_BY_PARAMETER);
		if (dto.getAshramcode() != null) {
			sb.append(" AND ASHRAM_CODE LIKE '%" + dto.getAshramcode()
					+ "%' ");
		}
		if (dto.getAshramName() != null) {
			sb.append(" AND ASHRAM_NAME LIKE '%" + dto.getAshramName()
					+ "%' ");
		}
		if (dto.getAshramAddress() != null) {
			sb.append(" AND ASHRAM_ADDRESS_ID LIKE '%" + dto.getAshramAddress() + "%' ");

		}
		if (dto.getAshramContactNo() != null) {
			sb.append(" AND ASHRAM_CONTACT_NO LIKE '%" + dto.getAshramContactNo()
					+ "%' ");
		}
		if (dto.getEmail() != null) {
			sb.append(" AND EMAIL LIKE '%" + dto.getEmail() + "%' ");
		}
		if (dto.getPrimarycontact() != null) {
			sb.append(" AND PRIMARY_CONTACT LIKE '%" + dto.getPrimarycontact()
					+ "%' ");

		}
		if (dto.getSecondarycontact() != null) {
			sb.append(" AND SECONDARY_CONTACT LIKE '%" + dto.getSecondarycontact()
					+ "%' ");

		}
		if (dto.getFax() != null) {
			sb.append(" AND FAX LIKE '%" + dto.getFax() + "%' ");

		}
		if (dto.getStartDate() != null) {
			sb.append(" AND START_DATE LIKE '%" + dto.getStartDate()
					+ "%' ");

		}
		if (dto.getOwnOrRent() != null) {
			sb.append(" AND OWN_OR_RENT LIKE '%" + dto.getOwnOrRent()
					+ "%' ");

		}
		if (dto.getIsDeleted() != null) {
			sb.append(" AND IS_DELETED LIKE '%" + dto.getIsDeleted()
					+ "%' ");

		}
		if (dto.getComment() != null) {
			sb.append(" AND COMMENT LIKE '%" + dto.getComment() + "%' ");

		}
		if (dto.getLastUpdatedBy() != null) {
			sb.append(" AND LAST_UPDATED_BY LIKE '%" + dto.getLastUpdatedBy()
					+ "%' ");

		}
		if (dto.getLastUpdatedOn() != null) {
			sb.append(" AND LAST_UPDATED_ON LIKE '%" + dto.getLastUpdatedOn()
					+ "%' ");

		}

		System.out.println(sb.toString());
		return (List<VyAshramDO>) QueryManager.executeListQuery(dto, sb
				.toString());
	}

}
